package com.entities;

import java.util.ArrayList;
import java.util.List;

import com.unit4science.Main;

public class Chart {
	String name;
	List<Unit> units;
	float[][] data;

	public Chart() {
	}

	public Chart(String name) {
		this.name = name;
	}

	public Chart(String name, List<Unit> units) {
		this.name = name;
		this.units = units;
	}

	public Unit getUnit(int index) {
		return this.units.get(index);
	}

	public List<Unit> getUnits(int resourceUnitName, int resourceUnitShortname) {
		List<Unit> list = new ArrayList<Unit>();

		String[] values = Main.getContext().getResources()
				.getStringArray(resourceUnitName);

		String[] values_short = Main.getContext().getResources()
				.getStringArray(resourceUnitShortname);

		for (int i = 0; i < values_short.length; i++) {
			list.add(new Unit(values[i], values_short[i]));
		}

		return list;
	}

	public int getIndexOfUnit(Unit unit) {
		int index = -1;
		int size = this.units.size();
		for (int i = 0; i < size; i++) {
			if (this.units.get(i).getName().equalsIgnoreCase(unit.getName())) {
				index = i;
				break;
			}
		}
		return index;
	}

	/**
	 * 
	 * @param p1
	 * @param p2
	 * @return ti le chenh lech giua hai don vi
	 */
	public float convert(int p1, int p2) {
		float result = 1f;

		if (data[p1][p2] != 0) {
			return data[p1][p2];
		}

		for (int i = data.length - 1; i > p1; i--) {
			if (data[p1][i] != 0) {
				result = data[p1][i] * convert(i, p2);
				break;
			}
		}

		return result;
	}

	float y = 1;

	public float convertTemperature(int p1, int p2) {
		float result = 1f;
		if (data[p1][p2] != 0) {
			if (data[p1][p2] < 2) {
				return y * data[p1][p2];
			} else {
				return y + data[p1][p2];
			}
		}

		for (int i = p1 + 1; i < data.length; i++) {			
			if (data[p1][i] != 0) {
				if (data[p1][i] < 2) {
					y = y * data[p1][i];					
				} else {
					y = data[p1][i] + y;					
				}
				convertTemperature(i,p2);
				break;
			}
		}

		return result;
	}

	public float convert(int p1, int p2, float number) {
		if (p1 < p2) {
			return number * convert(p1, p2);
		} else {
			return number / convert(p2, p1);
		}
	}

	public float[][] getData() {
		return data;
	}

	public void setData(float[][] data) {
		this.data = data;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public List<Unit> getUnits() {
		return units;
	}

	public void setUnits(List<Unit> units) {
		this.units = units;
	}

	public static List<Chart> getCharts() {
		// khoi tao danh sach cac Charts
		List<Chart> charts = new ArrayList<Chart>();

		charts.add(new Acceleration());
		charts.add(new Angle());
		charts.add(new AngularVelocity());
		charts.add(new Area());
		charts.add(new Capacitance());
		charts.add(new Charge());
		charts.add(new Conductance());
		charts.add(new CurrentDensity());
		charts.add(new Density());
		charts.add(new ElectricCurrent());
		charts.add(new ElectricFieldStrength());
		charts.add(new ElectromotiveForce());
		charts.add(new Energy());
		charts.add(new Flow());
		charts.add(new Force());
		charts.add(new Frequency());
		charts.add(new Inductance());
		charts.add(new Length());
		charts.add(new Luminance());
		charts.add(new MassResistivity());
		charts.add(new Resistance());
		charts.add(new Temperature());
		charts.add(new VolumeResistivity());

		return charts;
	}

}
